/***********************************************************************************************************************
**	filename: dbg.c
**	author: Echo Zhang @2021-05-08
**  e-mail: not_xx@126.com
***********************************************************************************************************************/

#include "dbg.h"
#include "tick.h"

#if defined(USING_PRINTF)

#if !defined(USING_STD_PRINTF)
#include "fio.h"
#if defined(__linux__)
int putchar(int c);
bool std_putc(ui8 ch, void *handle)
{
	putchar(ch);

	return true;
}
#endif
bool std_putc(ui8 ch, void *handle);
void std_printf(const char *fmt, ...)
{
	va_list vaList;

	va_start(vaList, fmt);
	fio_print(std_putc, NULL, fmt, vaList);
	va_end(vaList);
}
#endif

void _dbg_print_bytes_as_hex(const char *filename, int lineNum, const char *title, const void *bytes, Size_DT bytesLen)
{
	Size_DT i;
	std_printf("%s#%d %s [%d]: @%lu\n", filename, lineNum, title, bytesLen, tick_get());
	for(i=0; i<bytesLen; i++) {
		std_printf("%02hhX ", ((const ui8 *)bytes)[i]);
		if((i%16) == 15) {
			std_printf("\n");
		}
	}
	if(i%16 != 0) {
		std_printf("\n");
	}
}

void _dbg_print_bytes_as_string(const char *filename, int lineNum, const char *title, const void *bytes, Size_DT bytesLen)
{
	std_printf("%s#%d %s [%d]:\n", filename, lineNum, title, bytesLen);
	while(bytesLen-- > 0) {
		std_printf("%c", *((const ui8 *)bytes));
		bytes = ((const ui8 *)bytes) + 1;
	}
	std_printf("\n");
}

#endif



/***********************************************************************************************************************
**	endline
***********************************************************************************************************************/
